<template>
	<view class="wf-box" :style="{ paddingTop: totalheight }">
		<view class="head" :style="{ paddingTop: navBarHeight }">
			<view class="head-np">
				<view class="back" @click="back">取消</view>
				<!-- {{  $t('__4__') }} 选择联系人 {{ type }}-->
				<view class="title" v-if="type == 1">添加成员</view>
				<view class="title" v-if="type == 2">移出成员</view>
				<!-- @click="createNewGroup" -->
				<view class="submit" @click="onInviteFriends" v-if="type == 1">完成</view>
				<view class="submit" @click="onKickOut" v-if="type == 2">移除</view>
			</view>


		</view>

		<view class="main">

			<view class="selected-box">
				<view v-for="item in selectUserArr">

					<!-- <image src="../../static/img/demo-01.png"></image> -->
					<image :src="item.headImage"></image>
				</view>
			</view>
			
			<!-- 添加成员 -->
			<view class="select-wrap" v-if="type == 1">
				<scroll-view scroll-y="true" class="scroll-Y">
					<view class="select-ht" v-for="item in friendItems" @click="selectUser(item)">
						<view class="slt-circle">
							
							<template v-if="item.disabled">
								<image src="../../static/img/chat-07.png" v-if="item.checked"></image>
							</template>	 
							
							<template v-else>
								<image src="../../static/img/chat-08.png" v-if="selectIdList.indexOf(item.id) == -1"></image>
								<image src="../../static/img/chat-07.png" v-else></image>
							</template>
							 
						</view>
						<view class="slt-info">
							<image :src="item.headImage"></image>
							<text>{{ item.nickName }}</text>
						</view>
						<!-- <image :src="item.url" style="width: 60rpx; height: 60rpx;"></image> -->
					</view>
				</scroll-view>
			</view>
			
			
			<!-- 移除成员 -->
			<view class="select-wrap" v-if="type == 2">
				<scroll-view scroll-y="true" class="scroll-Y">
					<view class="select-ht" v-for="item in groupMembers" @click="selectRemove(item)" v-show="isSelf(item.userId)">
						<view class="slt-circle">
							<!-- <image src="../../static/img/chat-08.png" v-if="selectIdList.indexOf(item.userId) == -1"></image> -->
							<!-- <image src="../../static/img/chat-07.png" v-else></image> -->
							<image src="../../static/img/chat-07.png" v-if="removeCurId == item.userId"></image> 
							<image src="../../static/img/chat-08.png" v-else></image>
						</view>
						<view class="slt-info">
							<image :src="item.headImage"></image>
							<text>{{ item.aliasName }}</text>
						</view>
						<!-- <image :src="item.url" style="width: 60rpx; height: 60rpx;"></image> -->
					</view>
				</scroll-view>
			</view>
		</view>
	</view>
</template>

<script>
	import { throttle } from '@/utils/common.js'
	import navBarMixin from '@/mixin/navBarMixin.js'
	import { friendListApi } from '@/api/api.js'
	export default {
		data() {
			return {
				type: '',//，1.添加成员，2.移除成员
				
				selectUserArr: [], // 选择用户的集合
				selectIdList: [], // 选择用户的id 
				removeCurId: '',
				
				groupId: null,
				groupMembers: [], // 群成员
				friendItems: []
			}
		},
		mixins: [navBarMixin],
		methods: {
			back() {
				uni.navigateBack()
			},
			
			// 邀请好友方法
			onInviteFriends() {
				throttle(() => {
					let inviteVo = {
						groupId: this.groupId,
						friendIds: this.selectIdList
					}
					if(this.selectIdList.length > 0) {
						this.$http({
							url: "/group/invite",
							method: 'POST',
							data: inviteVo
						}).then(() => {
							uni.showToast({
								title: '邀请成功',
								icon: 'none'
							})
							 setTimeout(() => {
								 let pages = getCurrentPages();
								 let prevPage = pages[pages.length - 2];
								 prevPage.$vm.loadGroupMembers();
								 uni.navigateBack();
							 },2000)
							
						})
					}else {
						uni.showToast({
							title: '请选择添加的成员',
							icon: 'none'
						})
					}
					
				},4000)
			},
			
			// 加载群成员
			loadGroupMembers(id) {
				this.$http({
					url: `/group/members/${id}`,
					method: "GET"
				}).then((members) => {
					this.groupMembers = members.filter(m => !m.quit);
					if(this.type == 1) {
						// 只有群 添加成员需要
						this.initFriendItems();
					}
					
					
				})
			},
			
			initFriendItems() {
				this.friendItems = [];
				let friends = this.$store.state.friendStore.friends;
				
				// console.log('friends===',friends)
				
				friends.forEach((f => {
					let item = {
						id: f.id,
						headImage: f.headImage,
						nickName: f.nickName,
						online: f.online
					}
					item.disabled = this.isGroupMember(f.id);
					item.checked = item.disabled;
					this.friendItems.push(item);
					console.log('this.friendItems==',this.friendItems)
					
				}))
			},
			
			isGroupMember(id) {
				return this.groupMembers.some(m => m.userId == id);
			},
	
			// 点击是否选择用户
			selectUser(item) {
				if(item.disabled) return; 
				
				if(this.selectIdList.indexOf(item.id) != -1) {
					this.selectIdList = this.selectIdList.filter(user => user != item.id)
					this.selectUserArr =  this.selectUserArr.filter(iv => iv.id != item.id)
					
					// console.log('this.selectIdList后===',this.selectIdList)
				}else {
					this.selectUserArr.push(item)
					 this.selectIdList.push(item.id)
				}
				
			},
			
			// 移除成员列表，要排除自己
			isSelf(userId) {
				return this.$store.state.userStore.userInfo.id != userId
			},
			
			selectRemove(item) {
				
				// if(this.selectIdList.indexOf(item.userId) != -1) {
				// 	this.selectIdList = this.selectIdList.filter(user => user != item.userId)
				// }else {
				// 	this.selectIdList.push(item.userId)
				// }
				
				// 现阶段 只能一个一个 移除 成员
				this.selectUserArr = [item]
				this.removeCurId = item.userId
			},
			
			
			onKickOut() {
				throttle(() => {
					this.$http({
						url: `/group/kick/${this.groupId}?userId=${this.removeCurId}`,
						method: 'DELETE'
					}).then(() => {
						uni.showToast({
							title: '移除成功',
							icon: 'none'
						})
						setTimeout(() => {
							let pages = getCurrentPages();
							let prevPage = pages[pages.length - 2];
							prevPage.$vm.loadGroupMembers();
							uni.navigateBack();
						},2000)
					})
				},4000)
			}
		},
		onLoad(options) {
			
			this.type = options.type
			this.groupId = options.id
		
			this.loadGroupMembers(options.id)
		}
	}
</script>

<style lang="scss" scoped>
	.wf-box {
		position: relative;
		overflow: hidden;

		.head {
			position: fixed;
			top: 0;
			z-index: 1;
			width: 100%;
			background: #fff;

			.head-np {
				display: flex;
				justify-content: space-between;
				align-items: center;
				width: 100%;
				height: 96rpx;
				padding: 0 30rpx;
				box-sizing: border-box;
			}

			.back {
				color: #333;
				font-size: 28rpx;
			}

			.title {
				font-size: 36rpx;
				font-weight: bold;
			}

			.submit {
				padding: 6rpx 24rpx;
				font-size: 28rpx;
				background: $theme-color-a;
				color: #fff;
				border-radius: 28rpx;
			}
		}

		.main {
			padding: 0 30rpx;
			padding-bottom: 60rpx;
			margin-top: 30rpx;
		}

		.selected-box {
			display: flex;
			min-height: 80rpx;
			// position: fixed;
			flex-wrap: wrap;
			padding: 10rpx 10rpx;
			background-color: #fff;
			border-radius: 6rpx;

			view {
				width: max-content;
				margin: 10rpx;

				image {
					width: 60rpx;
					height: 60rpx;
				}
			}

		}

		.select-wrap {
			height: 1000rpx;
			padding: 30rpx;
			margin-top: 30rpx;
			background: #fff;
			border-radius: 6rpx;
			box-sizing: border-box;

			.scroll-Y {
				height: 970rpx;
			}

			.select-ht {
				display: flex;
				align-items: center;
				padding-top: 30rpx;
				padding-bottom: 30rpx;

				.slt-circle {
					image {
						width: 30rpx;
						height: 30rpx;
					}
				}

				.slt-info {
					position: relative;
					flex: 1;
					display: flex;
					align-items: center;
					margin-left: 30rpx;

					image {
						width: 60rpx;
						height: 60rpx;
					}

					text {
						margin-left: 20rpx;
						color: #333;
					}

					&::after {
						content: '';
						position: absolute;
						width: 100%;
						height: 2rpx;
						background: #eee;
						bottom: -30rpx;
					}
				}

				&:first-child {
					padding-top: 0;
				}
			}
		}

	}
</style>